<?php
class AdminPreConfiguredDesignController extends ModuleAdminController 
{
    public function __construct() 
    {
        $this->table 	  = 'pt_preconfigured_design';
		$this->identifier = 'id_design';
        $this->className  = 'PreConfiguredDesign';
		
		$this->context = Context::getContext();
		
		$this->addRowAction('view');
		$this->addRowAction('edit');
		$this->addRowAction('delete');
		
		$this->bulk_actions = array(
			'delete' => array(
				'text' => $this->l('Delete selected'), 
				'confirm' => $this->l('Delete selected items?')
			)
		);
        
        $this->fields_list = array(
			'id_design' => array(
				'title' => $this->l('ID'), 
				'align' => 'center', 
				'width' => 25
			),
            'id_cms' => array(
                'title' 	=> $this->l('CMS'),
                'callback' 	=> 'getCMSName'
            ),
            'namespace' => array(
                'title' 	=> $this->l('Namespace')
            ),
            'key_name' => array(
                'title' 	=> $this->l('Value'),
            ),
    		'default' => array(
    			'title' => $this->l('Is Default'),
    			'width' => 25,
    			'align' => 'center',
    			'type' => 'bool',
    			'tmpTableFilter' => true,
    			'icon' => array(
    				'1' => 'enabled.gif',
    				'0' => 'disabled.gif'
    			),
    			'orderby' => false
    		),
			'active' => array(
				'title' => $this->l('Displayed'), 
				'width' => 25, 
				'align' => 'center', 
				'active' => 'status', 
				'type' => 'bool', 
				'orderby' => false
			)
        );

        parent :: __construct();
    }
	
	public function renderForm()
	{
		$this->fields_form = array(
			'tinymce' => true,
			'legend' => array(
				'title' => $this->l('Available Pre-configured design'),
				'image' => '../img/admin/cog.gif'
			),
			'input' => array(
				array(
					'type' => 'select',
					'label' => $this->l('Prestatheme CMS'),
					'name' => 'id_cms',
					'required' => true,
					'options' => array(
						'query' => PrestathemeCms::getPrestathemeCMS(),
						'id' => 'id_cms',
						'name' => 'fullname'
					)
				),
				array(
					'type' => 'text',
					'label' => $this->l('Namespace'),
					'name' => 'namespace',
					'size' => 33,
					'required' => true,
					'hint' => $this->l('Invalid characters:').' <>;=#{}',
					'desc' => $this->l('Design namespace')
				),
				array(
					'type' => 'text',
					'label' => $this->l('Value'),
					'name' => 'key_name',
					'size' => 33,
					'required' => true,
					'hint' => $this->l('Invalid characters:').' <>;=#{}',
					'desc' => $this->l('Option for this namespace')
				),
				array(
					'type' => 'select',
					'label' => $this->l('Type'),
					'name' => 'type',
					'required' => true,
					'options' => array(
						'query' => array(
                            array('key' => 'group', 'value' => $this->l('Group')),
                            array('key' => 'design', 'value' => $this->l('Design')),
                            array('key' => 'style', 'value' => $this->l('Style')),
                        ),
						'id' => 'key',
						'name' => 'value'
					)
				),
				array(
					'type' => 'text',
					'label' => $this->l('Selector'),
					'name' => 'target_element',
					'size' => 33,
					'required' => true,
					'hint' => $this->l('Invalid characters:').' <>;=#{}',
					'desc' => $this->l('The HTML element on which we will apply the design')
				),
				array(
					'type' => 'textarea',
					'label' => $this->l('Style'),
					'name' => 'style',
					'rows' => 5,
					'cols' => 40,
					'hint' => $this->l('Forbidden characters:').' <>;=#{}',
					'desc' => $this->l('The CSS code to be associated to the selector')
				),
				array(
					'type' => 'radio',
					'label' => $this->l('Is Default'),
					'name' => 'default',
					'required' => false,
					'class' => 't',
					'is_bool' => true,
					'values' => array(
						array(
							'id' => 'default_on',
							'value' => 1,
							'label' => $this->l('Yes')
						),
						array(
							'id' => 'default_off',
							'value' => 0,
							'label' => $this->l('No')
						)
					),
				),
				array(
					'type' => 'radio',
					'label' => $this->l('Available'),
					'name' => 'active',
					'required' => false,
					'class' => 't',
					'is_bool' => true,
					'values' => array(
						array(
							'id' => 'active_on',
							'value' => 1,
							'label' => $this->l('Enabled')
						),
						array(
							'id' => 'active_off',
							'value' => 0,
							'label' => $this->l('Disabled')
						)
					),
				),
			),
			'submit' => array(
				'title' => $this->l('   Save   '),
				'class' => 'button'
			)
		);

		if (!($obj = $this->loadObject(true)))
			return;

		return parent::renderForm();
	}

	public function renderView()
	{
		if (!($object = $this->loadObject()))
			return;
		
        $types_list = array('group' => $this->l('Group'), 'design' => $this->l('Design'), 'style' => $this->l('Style'));
        
		$this->tpl_view_vars = array(
			'id_lang' => Context::getContext()->language->id,
			'types_list' => $types_list,
			'object' => $object,
			'head_title' => ($object->id ? (self::getCMSName($object->id_cms) .' : '. $object->namespace) : $this->l('New Pre-configured Design')),
			'shopContext' => Shop::getContext(),
		);

		return parent::renderView();
	}

    public static function getCMSName ($id_cms)
    {
        $cms = new PrestathemeCms ($id_cms, Context::getContext()->language->id);
        if ($cms)
            return $cms->getCMSName();
        else
            return NULL;
    }
}